#include<stdio.h>
#include<conio.h>
void packCharacters(unsigned int *x,char c, unsigned int mask);
void unpackCharacters(unsigned int *x, char c, unsigned int mask);
void displayBits( unsigned value );


int main()
{
unsigned int x=0;
unsigned int mask = 255;
char c=' ';
packCharacters( &x,c,mask );
printf("-------------------------------------------------\n");
unpackCharacters( &x,c,mask );
return 0;

}

void packCharacters(unsigned int *x,char c, unsigned int mask)
{
	for(int i=0; i<4; i++)
	{
		printf("Please enter a character:");
		c=getch();
		printf("\n");

		displayBits(c);
		displayBits(*x);
		(*x)=*x | c;
		displayBits(*x);
		if(i==3)
			break;
		*x=*x<<8;
	}


}


void unpackCharacters(unsigned int *x,char c, unsigned int mask)
{
	printf("--Unpacking chars--");
	printf("\n");
	mask=mask<<24;

	printf("      ");

	for(int i=0; i<4; i++)
	{
		printf("%c ",(mask & (*x))>>((3-i)*8));
		mask=mask>>8;	
	}

	printf("\n\n");


}




void displayBits( unsigned value )
{
	unsigned c; /* counter */
/* declare displayMask and left shift 31 bits */
	unsigned displayMask = 1 << 31;
	printf( "%10u = ", value );
/* loop through bits */
	for ( c = 1; c <= 32; c++ )
	{
		putchar( value & displayMask ? '1' : '0' );
		value <<= 1; /* shift value left by 1 */
		if ( c % 8 == 0 ) { /* output a space after 8 bits */
		putchar( ' ' );
		} /* end if */
	} /* end for */

	putchar( '\n\n' );
}